package com.cn.zpark;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: RequestDemo02
 * @Description:
 * @author: RZS
 * @date: 2022/4/11  14:29
 */
@WebServlet("/rd2.do")
public class RequestDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应格式及编码
        response.setContentType("text/html;charset=utf-8");
        // 获取写出流对象
        PrintWriter writer = response.getWriter();
        writer.println("<table border='1' cellspacing='0' cellpadding='10'>");
        writer.println("<tr><th>请求头名字</th><th>请求值</th></tr>");

        // 获取请求头数据
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            // 获取请求头字段名字
            String headerName = headerNames.nextElement();
            // 获取请求头对应的值
            String header = request.getHeader(headerName);

            writer.println("<tr><td>"+headerName+"</td><td>"+header+"</td></tr>");

        }

        writer.println("</table>");
        // 关闭资源
        writer.close();
    }
}
